D:\a\tools.proto\tools.proto\dynamic\src\field\primitive\interface.rs
Line | Count | Source |
1 | | // Copyright (c) 2025, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use std::fmt::{Display, Formatter}; |
30 | | |
31 | | #[derive(Copy, Clone, Debug)] |
32 | | pub enum Value { |
33 | | Unsigned(u64), |
34 | | Signed(i64), |
35 | | Float(f64), |
36 | | Bool(bool) |
37 | | } |
38 | | |
39 | | impl Display for Value { |
40 | 22 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
41 | 22 | match self { |
42 | 14 | Value::Unsigned(v) => write!(f, "{:.4}u", v), |
43 | 4 | Value::Signed(v) => write!(f, "{:.4}i", v), |
44 | 4 | Value::Float(v) => write!(f, "{:.4}f", v), |
45 | 0 | Value::Bool(v) => write!(f, "{}b", v) |
46 | | } |
47 | 22 | } |
48 | | } |
49 | | |
50 | | impl From<i32> for Value { |
51 | 13 | fn from(value: i32) -> Self { |
52 | 13 | Self::Signed(value as i64) |
53 | 13 | } |
54 | | } |
55 | | |
56 | | impl From<u64> for Value { |
57 | 0 | fn from(value: u64) -> Self { |
58 | 0 | Self::Unsigned(value) |
59 | 0 | } |
60 | | } |
61 | | |
62 | | impl From<i64> for Value { |
63 | 0 | fn from(value: i64) -> Self { |
64 | 0 | Self::Signed(value) |
65 | 0 | } |
66 | | } |
67 | | |
68 | | impl From<f64> for Value { |
69 | 11 | fn from(value: f64) -> Self { |
70 | 11 | Self::Float(value) |
71 | 11 | } |
72 | | } |
73 | | |
74 | | impl From<bool> for Value { |
75 | 0 | fn from(value: bool) -> Self { |
76 | 0 | Self::Bool(value) |
77 | 0 | } |
78 | | } |
79 | | |
80 | | impl Value { |
81 | 16 | pub fn to_signed(&self) -> i64 { |
82 | 16 | match self { |
83 | 0 | Value::Unsigned(v) => *v as i64, |
84 | 16 | Value::Signed(v) => *v, |
85 | 0 | Value::Float(v) => *v as i64, |
86 | 0 | Value::Bool(v) => if *v { 1 } else { 0 } Branch (86:34): [Folded - Ignored]
Branch (86:34): [True: 0, False: 0]
|
87 | | } |
88 | 16 | } |
89 | | |
90 | 11 | pub fn to_unsigned(&self) -> u64 { |
91 | 11 | match self { |
92 | 4 | Value::Unsigned(v) => *v, |
93 | 5 | Value::Signed(v) => *v as u64, |
94 | 2 | Value::Float(v) => *v as u64, |
95 | 0 | Value::Bool(v) => if *v { 1 } else { 0 } Branch (95:34): [Folded - Ignored]
Branch (95:34): [True: 0, False: 0]
|
96 | | } |
97 | 11 | } |
98 | | |
99 | 12 | pub fn to_float(&self) -> f64 { |
100 | 12 | match self { |
101 | 7 | Value::Unsigned(v) => *v as f64, |
102 | 0 | Value::Signed(v) => *v as f64, |
103 | 5 | Value::Float(v) => *v, |
104 | 0 | Value::Bool(v) => if *v { 1.0 } else { 0.0 } Branch (104:34): [Folded - Ignored]
Branch (104:34): [True: 0, False: 0]
|
105 | | } |
106 | 12 | } |
107 | | |
108 | 0 | pub fn to_bool(&self) -> bool { |
109 | 0 | match self { |
110 | 0 | Value::Unsigned(v) => *v != 0, |
111 | 0 | Value::Signed(v) => *v != 0, |
112 | 0 | Value::Float(v) => *v != 0.0, |
113 | 0 | Value::Bool(v) => *v |
114 | | } |
115 | 0 | } |
116 | | } |
117 | | |
118 | | pub trait PrimitiveType { |
119 | | fn get_bin(&self, bytes: &[u8]) -> u64; |
120 | | fn set_bin(&self, bytes: &mut [u8], bin: u64); |
121 | | fn get_raw(&self, bytes: &[u8]) -> Value; |
122 | | fn set_raw(&self, bytes: &mut [u8], raw: Value); |
123 | | fn get(&self, bytes: &[u8]) -> Value; |
124 | | fn set(&self, bytes: &mut [u8], value: Value); |
125 | | } |